#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
const int N = 5010;
int n;
int a[N],f[N][N][2];

signed main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);

    int T;cin>>T;
    while(T--)
    {
        memset(f,0,sizeof f);
        cin>>n;
        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        for(int i=2;i<=n;i++)
            for(int j=0;j<i;j++)
        {
            if(j < i-1)
            {
                f[i][j][0] = max(f[i][j][0],f[i-1][j][1] + ((a[j]==a[i])?a[i]:0));

                f[i][j][1] = max(f[i][j][1],f[i-1][j][1] + ((a[i]==a[i-1])?a[i]:0));

                f[i][i-1][0] = max(f[i][i-1][0],f[i-1][j][0] + ((a[i-1]==a[i])?a[i]:0));

                f[i][i-1][1] = max(f[i][i-1][1],f[i-1][j][0] + ((a[i]==a[j])?a[i]:0));
            }
            else // j == i-1
            {
                f[i][j][0] = max(f[i][j][0],f[i-1][j][1] + ((a[j]==a[i])?a[i]:0));

                f[i][j][1] = max(f[i][j][1],f[i-1][j][1] + ((a[i]==a[i-1])?a[i]:0));

            }
           // printf("%lld %lld %lld %lld\n",i,j,f[i][j][0],f[i][j][1]);

        }
        int ans = 0;
        for(int i=1;i<n;i++)
            ans = max(ans,max(f[n][i][0],f[n][i][1]));
        printf("%lld\n",ans);
    }
    return 0;
}
